define(function(require,exports,module){
	/**--tmpl-require--**/
	
	//加载依赖
	require("ajaxproxy");
	var common = require("common");

	//初始化AjaxProxy插件
	$('.ajaxproxy').AjaxProxy({
		dataType : "json",
		method : "post",
		formId : "content-add-form",
		formCheckHandler : function(form_id) {
			return common.formCheckHandler(form_id, true);
		},
		callbackDelay : __global.jdialog.timer,
		timeInterval : __global.jdialog.timer,
		callBack : function (data) {   /* 执行ajax之后的回调函数 */
			common.ajaxCallback(data);
		}
	});

	exports.init = function(){
		/**--tmpl-todo--**/

		exports.assocadmin_role();


	};
	/**--tmpl-otherfunction--**/

	/**-- assocfield --**/
	exports.assocadmin_role = function(){
		if(typeof(exports.admin_role_selected_ids) == 'undefined'){
			exports.admin_role_selected_ids = new Array();
		}
		var currpage = 1;
		selectBtn = $("#assoc-search-admin_role").click(function(){
			JDialog.win.work({
				'title' : '选择',
				'content' : JTemplate.render('admin_role-assoc-box'),
				'width' : 800,
				'height' : 600,
				'borderWidth' : 8,
				'lock' : true,
				'effect' : 0,
				'button' : {
					'确定' : function(){
					},
					'取消': function(){
	
					}
				}
			});
	
			function assoc_search(searcharr){
				searcharr.page=currpage;
				$.get(exports.admin_role_assoc_url,searcharr,function(data){
					dom = JTemplate.render('admin_role-assoc-tablelist',data);
					pagetotal = Math.ceil(data.total / 15);
					p = {};
					p.pages = [];
					p.curr = parseInt(currpage);
					p.nexts = (p.curr + 1 <= pagetotal) ? p.curr + 1 : pagetotal;
					p.prev = p.curr - 1 >= 1		? p.curr - 1 : 1;
					for(i = 1;i <= 5; i ++){
						offset = currpage <= 2 ? 0 : (currpage >= pagetotal - 2 ? pagetotal - 5 : currpage - 3);
						if(i + offset > 0 && i + offset <= pagetotal)
							p.pages.push(i + offset);
					}
					page = JTemplate.render('assocpage',p);
					$('.assoc-alertbox .content').empty();
					$('.assoc-alertbox .content').append($(dom));
					$('.assoc-alertbox .content input').each(function(){
						for(var i = 0;i < exports.admin_role_selected_ids.length; i ++){
							if($(this).val() == exports.admin_role_selected_ids[i]){
								$(this).prop('checked',true);
							}
						}
					});
	
					$('.assoc-alertbox .paginate').empty();
					if(pagetotal >= 2)
					{
						$('.assoc-alertbox .paginate').append($(page));
					}
	
				},'json');
			};
			assoc_search({});
	
			$(".assoc-alertbox .assoc-search-btn").click(function(){
				var f = $(".assoc-alertbox .assoc-foreign-form").serializeArray();
				r = {};for(var i =0;i<f.length;i++){r[f[i].name] = f[i].value}
				currpage =  1;
				assoc_search(r);
			});
			$(".assoc-alertbox .paginate").on('click','li a',function(){
				currpage = $(this).attr('data-page');
				var f = $(".assoc-alertbox .assoc-foreign-form").serializeArray();
				r = {};for(var i=0;i< f.length;i++){r[f[i].name] = f[i].value}
				assoc_search(r);
				
			});
			$('.assoc-alertbox .content').on('click','#assoc-selectall',function(){
				if($(this).closest('table').find('tbody td input:checked').length > 0){
					$(this).closest('table').find('tbody td input:checked').each(function(){
						$(this).trigger('click');
					});
				}else{
					$(this).closest('table').find('tbody td input').not(':checked').each(function(){
						$(this).trigger('click');
					
					});
					
				}
			});
			$(".assoc-alertbox .content").on('click','tbody td input',function(){
				assocul = $("#assoc-ul-admin_role");
				sid = $(this).val();
				if($(this).prop('checked')){
					exports.admin_role_selected_ids.push(sid);
					ele = $('<li><input type="hidden" name="data[admin_role][]" value="'+sid+'">'+name+'<i class="glyphicon-remove glyphicon pull-right"></i></li>');
					assocul.append(ele);
				}else{
					assocul.find('li').each(function(){
						id = $(this).find('input').val();
						if(sid == id){
							$(this).remove();
						}
					});
					for(var i in exports.admin_role_selected_ids){
						if(sid == exports.admin_role_selected_ids[i]){
							exports.admin_role_selected_ids.splice(i,1);
						}
					}
				}
				if($(this).closest('tbody').find('td input:checked').length == $(this).closest('tbody').find('td input').length){
					$('#assoc-selectall').prop('checked',true);
				}
				else{
					$('#assoc-selectall').removeProp('checked');
				
				}
				
			});
			if($('.assoc-alertbox .content tbody td').find('input:checked').length == $('.assoc-alertbox .content tbody td').find('input').length){
				$('#assoc-selectall').prop('checked',true);
			}
			else{
				$('#assoc-selectall').removeProp('checked');
	
			}
	
		});
		searchInput = $("#assoc-admin_role");
		searchInput.blur(function(){
	
			setTimeout(function(){
				$("#search-container-admin_role").remove();
			},500);
		});
		searchInput.focus(function(){
			$(this).trigger('keyup');
		});
		
		$("#box-admin_role").on("click","#search-container-admin_role span",function(){
			console.log('aaaaaa');
	
			id = $(this).attr('data-id');
			console.log(id);
			name = $(this).html();
			assocul = $("#assoc-ul-admin_role");
			selectedInputs = assocul.find('input');
			for(i = 0 ;i < selectedInputs.length;i ++){
				if(id == $(selectedInputs[i]).val()){
					$("#search-container-admin_role").remove();
					return;
				}
			}
			exports.admin_role_selected_ids.push(id);
			ele = $('<li><input type="hidden" name="data[admin_role][]" value="'+id+'">'+name+'<i class="glyphicon-remove glyphicon pull-right"></i></li>');
			assocul.append(ele);
			$("#search-container-admin_role").remove();
		});
		$("#assoc-ul-admin_role").on('click','li i',function(){
			id = $(this).closest('li').find('input').val();
			for(var i in exports.admin_role_selected_ids){
				if(id == exports.admin_role_selected_ids[i]){
					exports.admin_role_selected_ids.splice(i,1);
				}
			}
			$(this).closest('li').remove();
		});
		$("#assoc-admin_role").keyup(function(){
			var val = $(this).val();
			$.get(exports.admin_role_assoc_url ,{'kw' : val,page : 1,pagesize :15},function(data){
				searchInput = $("#assoc-admin_role");
	
	
				$("#search-container-admin_role").remove();
				container = $("<div class='assocview' id='search-container-admin_role'></div>");
				if(data.items){
					attr1 = '';
					attr2 = '';
					attr = "";
					for(var j in data.items[0]){
						if(j.indexOf('name') != -1){
							attr1 = j;
						}
						if(j.indexOf('title') != -1){
							attr2 = j;
						}
					}
					attr = attr1 ? attr1 : attr2;
					for(i = 0;i < data.items.length;i ++){
						item = $("<span data-id='"+data.items[i]['id']+"'>"+data.items[i][attr]+"</span>");
						container.append(item);
					}
					item = $("<em>共找到"+parseInt(data.total)+"条结果</em>");
					container.append(item);
					searchInput.after(container);
				}
	
			},'json');
		});
	};
	

});
